package com.jml.mapreduce.wordcount;

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

/**
 * 代码写完后把jar包上传到服务器然后运行
 * yarn jar mapxxx.jar  com.jml.mapreduce.wordcount.WordcountDriver  HDFS的输入   HDFS的输出
 */
public class WordcountDriver {

    /**
     * 处理的本地文件地址
     */
    public static String inPath = "D:\\jml-downtemp\\Downloads\\README.txt";
    /**
     * 本地的输出文件夹
     */
    public static String outPath = "/myout";

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        if(args.length>0){
            inPath = args[0];
            outPath = args[1];
        }
        // 1 获取配置信息以及封装任务
        Configuration configuration = new Configuration();
        Job job = Job.getInstance(configuration);
        // 2 设置jar加载路径
        job.setJarByClass(WordcountDriver.class);
        
        // 3 设置map和reduce类
        job.setMapperClass(WordcountMapper.class);
        job.setReducerClass(WordcountReducer.class);
        // 4 设置map输出
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        // 5 设置最终输出kv类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        // 6 设置输入和输出路径
        FileInputFormat.setInputPaths(job, new Path(inPath));
        FileOutputFormat.setOutputPath(job, new Path(outPath));
        //设置reduce的数量
        //job.setNumReduceTasks(10);
        // 7 提交
        boolean result = job.waitForCompletion(true);
        System.exit(result ? 0 : 1);
    }
}
